package com.dunjx.rabbitMq.work.lunxun;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class Provoiderword {


    public static Connection getConnection() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setHost("39.103.219.84");
        connectionFactory.setVirtualHost("/");
        return connectionFactory.newConnection("测试链接");
    }

    @Test
    public static void provider01() throws Exception {

        Connection connection = null;
        Channel channel = null;
        try {
            connection = getConnection();
            String channelName = "queue01";

            String fanoutExchangeName = "";
            String routingKey = "com.order.course.user.*";
            String exchangeType = "direct";
            channel = connection.createChannel();

            for (int i = 0; i < 20; i++) {
                String message = "topic 只有1 3 4 能收到 "+i;
                // TODO  注意 如果 交换机为空  第二个参数 就是指定队列 而非 路由key
                channel.basicPublish(fanoutExchangeName, channelName, null, message.getBytes(StandardCharsets.UTF_8));
            }
            System.out.println("消息发送成功");
        } catch (Exception e) {
            System.out.println("发送消息发生异常");
            e.printStackTrace();
        }

        if (channel != null && channel.isOpen()) {
            channel.close();
        }
        if (connection != null && connection.isOpen()) {
            connection.close();
        }
    }
}
